<template>
  <div class="container">
    <div>
      <el-form ref="form" :model="form">
        <el-form-item label="所属分组：" label-width="110px">
          <el-select
            v-model="form.CLIENT_NO"
            placeholder="请选择"
            clearable
            filterable
            @change="getVehicleList"
          >
            <el-option
              v-for="item in groupingList"
              :key="item.CLIENT_NO"
              :label="item.CLIENT_NAME"
              :value="item.CLIENT_NO"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车辆名称：" label-width="110px">
          <el-select
            v-model="form.SIMCARD_NO"
            placeholder="请选择"
            clearable
            filterable
          >
            <el-option
              v-for="item in vehicleList"
              :key="item.CARDNO"
              :label="item.LICENSEPLATE"
              :value="item.CARDNO"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="定位间隔：" label-width="110px">
          <el-input
            v-model="form.TEXTCONTENT"
            placeholder="请输入"
            style="width: 100%"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-row type="flex" justify="center">
        <el-col :span="24"
          ><div align="center">
            <el-button
              type="primary"
              @click="saveAdd"
              style="width: 100%; margin-bottom: 18px"
            >
              定 位 间 隔 设 置</el-button
            >
          </div></el-col
        >
      </el-row>
    </div>
    <el-divider>已设置车辆</el-divider>
    <div>
      <el-row type="flex" justify="space-around">
        <el-col :span="5">
          <el-form ref="select" :model="select" label-width="80px">
            <el-form-item label="车辆名称：" style="width: 100%">
              <el-input
                v-model="select.LICENSE_PLATE"
                placeholder="请输入车辆名称"
              ></el-input>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="5">
          <el-form ref="select" :model="select" label-width="80px">
            <el-form-item label="所属分组：" style="width: 100%">
              <el-select
                v-model="select.groupingValue"
                :options="groupingList"
                placeholder="请选择所属分组"
                clearable
              >
                <el-option
                  v-for="(item, index) in groupingList"
                  :value="item.CLIENT_NO"
                  :label="item.CLIENT_NAME"
                  :key="index"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="5">
          <el-form ref="select" :model="select" label-width="80px">
            <el-form-item label="车辆类型：" style="width: 100%">
              <el-select
                v-model="select.carTypeValue"
                placeholder="请选择车辆类型"
                clearable
              >
                <el-option
                  v-for="(item, index) in selectCarType"
                  :value="item.label"
                  :label="item.value"
                  :key="index"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="5">
          <el-form ref="select" :model="select" label-width="80px">
            <el-form-item label="在线状态：" style="width: 100%">
              <el-select
                v-model="select.statusValue"
                :options="statusOptions"
                placeholder="请选择在线状态"
                clearable
              >
                <el-option
                  v-for="(item, index) in statusOptions"
                  :value="item.value"
                  :label="item.label"
                  :key="index"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <el-row type="flex" justify="space-around">
        <el-col :span="5">
          <el-form ref="select" :model="select" label-width="80px">
            <el-form-item label="设置状态：" style="width: 100%">
              <el-select
                v-model="select.lockValue"
                :options="lockOptions"
                placeholder="请选择设置状态"
                clearable
              >
                <el-option
                  v-for="(item, index) in lockOptions"
                  :value="item.value"
                  :label="item.label"
                  :key="index"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="3">
          <span style="font-size: small">设置成功：{{ this.setNum }}辆</span>
        </el-col>
        <el-col :span="7">
          <el-form ref="form" :model="form" style="float: left">
            <span style="font-size: small">在线数：{{ this.onlineNum }}辆</span>
            <span style="margin-left: 15px; font-size: small"
              >离线数：{{ this.offlineNum }}辆</span
            >
            <span style="margin-left: 15px; font-size: small"
              >总数：{{ this.carNum }}辆</span
            >
          </el-form>
        </el-col>
        <el-col :span="5">
          <el-form ref="form" :model="form" style="float: left">
            <el-button size="mini" type="primary" @click="getlistdata"
              >查 询</el-button
            >
            <el-button size="mini" type="success" @click="exportExcel"
              >导 出</el-button
            >
          </el-form>
        </el-col>
      </el-row>
    </div>
    <el-table
      highlight-current-row
      :data="
        tableData.slice((currentPage - 1) * pageSize, currentPage * pageSize)
      "
      border
      @sort-change="sortChange"
    >
      <el-table-column
        prop="LICENSE_PLATE"
        label="车辆名称"
        align="center"
        width="130"
      ></el-table-column>
      <el-table-column
        prop="CAR_TYPE"
        label="车辆类型"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="CLIENT_NAME"
        label="所属分组"
        align="center"
        width="130"
      ></el-table-column>
      <el-table-column
        prop="TEXTCONTENT"
        label="内容"
        align="center"
        width="300"
      ></el-table-column>
      <el-table-column
        prop="ONLINE"
        label="在线状态"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="STATUS"
        label="设置状态"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="BACK"
        label="返回信息"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="SubmitDateTime"
        label="提交时间"
        align="center"
        width="150"
        sortable="custom"
      ></el-table-column>
      <el-table-column
        prop="DisposeDateTime"
        label="生效时间"
        align="center"
        width="150"
        sortable="custom"
      ></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="handleEdit(scope.$index, scope.row)"
            >设置</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :current-page="currentPage"
        :page-size="pageSize"
        :total="tableData.length"
        @size-change="handleSizeChange"
        @current-change="handlePageChange"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import '@/assets/css/common.css'

import export2Excel from '@/utils/exportExcel/index.js'
import OSS from 'ali-oss';
import { time } from 'echarts';
export default {
  data () {

    return {
      currentPage: 1, //初始页
      pageSize: 10,    //    每页的数据
      select: {
        LICENSE_PLATE: '',
        groupingValue: '',
        carTypeValue: '',
        statusValue: '',
        lockValue: '3',
      },
      form: {
        CLIENT_NO: '00000',
        SIMCARD_NO: '',
        TEXTCONTENT: '#_ZTTtsSet;其它设置;定位包回传间隔=15$',
      },
      //排序列
      sort_column: '',
      //升序或倒序
      sort_type: '',
      //排序方式
      sort_num: 2,
      //设置成功
      setNum: 0,
      //车辆总数
      carNum: 0,
      //车辆在线数
      onlineNum: 0,
      //车辆离线数
      offlineNum: 0,
      tableData: [],
      groupingList: [],
      vehicleList: [],
      // 车辆类型列表
      selectCarType: [
        { value: '洗路车', label: '洗路车' },
        { value: '挤压车', label: '挤压车' }
      ],
      // 状态列表
      statusOptions: [
        { value: '0', label: '在线' },
        { value: '6', label: '离线' }
      ],
      // 设置列表
      lockOptions: [
        { value: '3', label: '全部' },
        { value: '0', label: '已下发' },
        { value: '1', label: '设置成功' },
        { value: null, label: '未设置' },
      ],
    }
  },
  mounted () {
    this.getGroupingList()
    this.getVehicleList()
    this.getlistdata()
  },
  methods: {

    // 获得所属分组列表
    async getGroupingList () {
      var url = 'Vehicle/GetGroupList?ClientNo=' + localStorage.getItem('CLIENT_NO') // zxdata
      let { data } = await this.$Http.get(url)
      this.groupingList = data
    },
    // 获得车辆列表
    async getVehicleList () {
      this.form.SIMCARD_NO = ''
      var url = 'Vehicle/GetCarListCurrentVehicle?CLIENTNO=' + this.form.CLIENT_NO + '&LICENSEPLATE=&CARGROUP=&CARSTATUS=1&CARTYPE='
      let { data } = await this.$Http.get(url)
      this.vehicleList = data
    },
    // 获取车辆信息列表
    async getlistdata () {
      var url = 'Vehicle/GetVehicleDeviceSettingInfo?TYPE=' + this.sort_num
      let { data } = await this.$Http.get(url)
      data.forEach(element => {
        //在线状态
        switch (element.isOnLine) {
          case '0':
            element.ONLINE = '在线'
            break
          case '6':
            element.ONLINE = '离线'
            break
          default:
            break
        }
        //设置状态
        switch (element.Status) {
          case '0':
            element.STATUS = '已下发'
            break
          case '1':
            element.STATUS = '设置成功'
            break
          default:
            break
        }
        //返回信息
        switch (element.BACKINFO) {
          case '0':
            element.BACK = '成功'
            break
          case '1':
            element.BACK = '失败'
            break
          case '2':
            element.BACK = '消息有误'
            break
          case '3':
            element.BACK = '不支持'
            break
          default:
            break
        }

      });
      this.originalData = data
      this.onlineNum = data.filter(item => item.isOnLine == '0').length;
      this.offlineNum = data.filter(item => item.isOnLine == '6').length;
      this.setNum = data.filter(item => item.Status == '1').length;
      this.carNum = data.length
      this.screeningData()
    },
    // 模糊查询
    screeningData () {
      this.tableData = this.originalData
      if (this.select.LICENSE_PLATE) this.tableData = this.tableData.filter(item => item.LICENSE_PLATE.includes(this.select.LICENSE_PLATE))
      if (this.select.groupingValue) this.tableData = this.tableData.filter(item => item.CLIENT_NO.includes(this.select.groupingValue))
      if (this.select.carTypeValue) this.tableData = this.tableData.filter(item => item.CAR_TYPE.includes(this.select.carTypeValue))
      if (this.select.statusValue) this.tableData = this.tableData.filter(item => item.isOnLine.includes(this.select.statusValue))
      if (this.select.lockValue !== '3') {
        this.tableData = this.tableData.filter(item => item.Status === this.select.lockValue)
      }
      console.log(this.select.groupingValue)
    },
    // 打开编辑窗口
    handleEdit (index, row) {
      this.form = Object.assign({}, row);
      this.form.SIMCARD_NO = row.SIMCARD_NO1;
      this.form.TEXTCONTENT = '#_ZTTtsSet;其它设置;定位包回传间隔=15$';
    },
    // 保存编辑
    async saveAdd () {
      this.$axios({
        url: 'Vehicle/IssuedInsVD',
        method: 'post',
        data: {
          SIMCARD_NO: this.form.SIMCARD_NO,
          TEXTCONTENT: this.form.TEXTCONTENT,
        },

      }).then((res) => {
        console.log(this.data)
        console.log(res)
        if (res.data.IsSucess) {
          this.$message.success('设置成功');
          this.getVehicleList()
          this.getlistdata()
        } else {
          this.$message.error('设置失败');
        }
      })
    },
    // 导出Excel
    exportExcel () {
      let excelName = '车辆定位间隔设置记录'
      const tHeader = ['车辆名称', '车辆类型', '内容', '在线状态', '设置状态', '返回信息', '提交时间', '生效时间']
      const filterVal = ['LICENSE_PLATE', 'CAR_TYPE', 'TEXTCONTENT', 'ONLINE', 'STATUS', 'BACK', 'SubmitDateTime', 'DisposeDateTime']
      export2Excel(tHeader, filterVal, this.tableData, excelName)
    },
    // 排序
    sortChange (e) {
      console.log(e, "e")
      if (e.order) {
        this.sort_column = e.prop;
        this.sort_type = e.order;
      } else {
        this.sort_column = ''
        this.sort_type = '';
      }
      // 1下发时间正序，2下发时间倒序，3生效时间正序，4生效时间倒序
      if (this.sort_column === 'SubmitDateTime' && this.sort_type === 'ascending') {
        this.sort_num = 1;
      } else if (this.sort_column === 'SubmitDateTime' && this.sort_type === 'descending') {
        this.sort_num = 2;
      } else if (this.sort_column === 'DisposeDateTime' && this.sort_type === 'ascending') {
        this.sort_num = 3;
      } else if (this.sort_column === 'DisposeDateTime' && this.sort_type === 'descending') {
        this.sort_num = 4;
      }
      this.getlistdata();
    },
    // 分页导航
    handlePageChange (currentPage) {
      this.currentPage = currentPage;
      // console.log(this.currentPage)
      // this.$set(this.query, 'pageIndex', val);
      this.getlistdata();
    },
    handleSizeChange (size) {
      this.pageSize = size;
      console.log(this.pageSize)
    }
  }
}
</script>
<style>
.el-form-item__label {
  text-align: left;
}
/* .el-checkbox {
  color: #fff;
}
.el-checkbox__label {
  color: #fff !important;
} */
.content {
  overflow-y: scroll;
}
.el-col-6 {
  width: 350px;
  min-height: 50px;
}
.el-table th {
  height: 40px;
}
.el-form-item label {
  width: 80px;
}
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  height: 32px;
  line-height: 30px;
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
.aroundtop {
  padding: 10px;
  background-color: #007acc;
  height: 100%;
  border-radius: 5px;
}
.el-icon-speed {
  color: #fff;
  font-size: 18px;
  font-weight: bold;
  height: 30px;
  line-height: 28px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  margin: 10px 20px 10px 20px;
}
</style>
